package cn.ronghuanet.controller;

import cn.ronghuanet.domain.Order;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @GetMapping("/user/{uid}")
    //SentinelResource是sentinel的资源,如果限流了要给他一个托底处理(getOrderByUidBlockHandler托底方法)
    @SentinelResource(value = "getOrderByUid", blockHandler = "getOrderByUidBlockHandler")
    public List<Order> getOrderByUid ( @PathVariable("uid") Long uid ) {
        //  if (uid==1){ int i = 1/0;}

        return Arrays.asList(new Order(1L, "order1", uid),
                new Order(2L, "order2", uid));
    }

    public List<Order> getOrderByUidFallback (
            @PathVariable("uid") Long uid ) {
        System.out.println("熔断降级了........");
        return Arrays.asList(new Order(-1L, "order1", uid));
    }

    public List<Order> getOrderByUidBlockHandler (
            @PathVariable("uid") Long uid, BlockException ex ) {
        ex.printStackTrace();
        System.out.println("限流了...");
        return Arrays.asList(new Order(-1L, "order1", uid));
    }
}
